<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Js多物体动画</title>
    <style>
        body{
            margin: 0;
            padding: 0;
        }
        ul,li{
            list-style: none;
        }
        ul li{
            width: 200px;
            height: 200px;
            background: yellow;
            margin-bottom: 20px;
            border: 4px solid #000;
        } 
    </style>
    <script>
        window.onload = function(){
            let aLi = document.getElementsByTagName('li');
            for(element of aLi){
                element.timer = null;
                element.onmouseover = function(){
                    startMove(this, 400);
                }
                element.onmouseout = function(){
                    startMove(this, 200);
                }
            };
        }
        function startMove(obj, iTarget){
            console.log('startMove',iTarget)
            clearInterval(obj.timer);
            obj.timer = setInterval(function(){
                let curWidth = parseInt(getStyle(obj,'width'))
                let speed = (iTarget-curWidth) / 8;
                speed = speed > 0 ? Math.ceil(speed): Math.floor(speed);
                if(curWidth === iTarget){
                    clearInterval(obj.timer);
                } else{
                    obj.style.width = curWidth + speed + 'px';
                }
            }, 30)
        }
        // 获取样式,记录width，避免有边界情况下出现的宽度越来越宽的bug,offsetWidth造成的
        function getStyle(obj, attr){
            if(obj.currentStyle){
                return obj.currentStyle[attr];
            }
            return getComputedStyle(obj,false)[attr];
        }

    </script>
</head>
<body>
    <ul>
        <li></li>
        <li></li>
        <li></li>
    </ul>
</body>
</html>